package ua.races.dom.docBuilder;

import org.jdom.Element;
import ua.races.vo.Bet;

import java.util.List;
import java.util.ListIterator;

public class BetDocumentBuilder extends DocumentBuilder {

    protected List<Bet> bets;

    public BetDocumentBuilder(List<Bet> bets) {
        this.bets = bets;
    }

    public void build() {
        Bet bet = null;

        Element root = new Element("bets");

        ListIterator<Bet> iterator = bets.listIterator();

        while (iterator.hasNext()) {
            Element betElement = new Element("bet");
            bet = iterator.next();

            Element betId = new Element("BetID");
            betId.setText(String.valueOf(bet.getBetId()));
            betElement.addContent(betId);

            Element raceId = new Element("RaceId");
            raceId.setText(String.valueOf(bet.getRaceId()));
            betElement.addContent(raceId);

            Element clientId = new Element("ClientID");
            clientId.setText(String.valueOf(bet.getClientId()));
            betElement.addContent(clientId);

            Element betDate = new Element("BetDate");
            betDate.setText(String.valueOf(bet.getBetDate()));
            betElement.addContent(betDate);

            Element betValue = new Element("BetValue");
            betValue.setText(String.valueOf(bet.getBetValue()));
            betElement.addContent(betValue);

            Element betTypeId = new Element("BetTypeID");
            betTypeId.setText(String.valueOf(bet.getBetTypeId()));
            betElement.addContent(betTypeId);

            Element firstHorse = new Element("FirstHorse");
            firstHorse.setText(String.valueOf(bet.getFirstHorse()));
            betElement.addContent(firstHorse);

            Element secondHorse = new Element("SecondHorse");
            if (bet.getSecondHorse() != null) {
                secondHorse.setText(String.valueOf(bet.getSecondHorse()));
            } else {
                secondHorse.setText("");
            }
            betElement.addContent(secondHorse);

            Element thirdHorse = new Element("ThirdHorse");
            if (bet.getThirdHorse() != null) {
                thirdHorse.setText(String.valueOf(bet.getThirdHorse()));
            } else {
                thirdHorse.setText("");
            }
            betElement.addContent(thirdHorse);

            root.addContent(betElement);
        }

        document.addContent(root);

    }
}
